﻿using System;
using System.Collections.Generic;
using System.Text;
using Pixysoft.Framework.Reflection;
using Pixysoft.Weblications.Sango.Environment;
using Pixysoft.Weblications.Sango.Dao;
using Pixysoft.Weblications.Sango.Protocol;
using Pixysoft.Weblications.Sango.Protocol.stage;
using Pixysoft.Weblications.Sango.Formular;
using Pixysoft.Weblications.Sango.Constant;
using Pixysoft.Weblications.Sango.Battle;

namespace Pixysoft.Weblications.Sango.Command.stage
{
    /// <summary>
    /// 获取当前地图 关卡列表
    /// </summary>
    partial class SangoStageHandler
    {
        public stage_list stage_list(double geox, double geoy)
        {
            string userCode = SangoManager.Authentication();

            stage_list response = ReflectionManager.CreatePojo<stage_list>();
            response.Succeed = false;

            Entities.Sys_Section sysSection = SysSectionHelper.trySearchSectionByGeolocation(geox, geoy);

            Entities.Usr_Profile profile = UsrProfileDao.getUsrProfile(userCode);
            Entities.Usr_Property property = UsrPropertyDao.getProperty(userCode);
            long mapcode = property.Mapserialcode;

            List<Entities.Usr_Stage> stages = StageDao.getStagesBySerialcode(userCode, mapcode, sysSection.Sectionserialcode);

            List<Entities.War_Stage> warstages = WarStageHelper.searchStagesByMap(mapcode);

            List<stage_stage> bodies = new List<stage_stage>();

            //选择最新没有打过的地图 以及可以重复攻打的地图

            if (stages.Count == 0)
            {
                bodies.Add(StageFacade.toStage(WarStageHelper.searchStartStage(mapcode), profile));
            }
            else
            {
                foreach (Entities.War_Stage stage in warstages)
                {
                    //过滤开始关卡

                    if (stage.Locationtypecode == WarStageLocationTypecodeCollection.START)
                        continue;

                    //过滤前置没有攻打的关卡

                    if (trySearchStageByIndex(stages, stage.Stageindex - 1) == null)
                        continue;

                    Entities.Usr_Stage usrstage = trySearchStageByCode(stages, stage.Stageserialcode);

                    if (usrstage == null)
                    {
                        bodies.Add(StageFacade.toStage(stage, profile));
                    }
                    else
                    {
                        //过滤无法重复攻打的关卡

                        if (StageFormular.CanRepeatAttack(stage))
                        {
                            bodies.Add(StageFacade.toStage(stage, usrstage));
                        }
                    }
                }
            }

            response.hasbattleresult = UsrRveBattleDao.GetRveBattlesCount(userCode) > 0;

            Entities.War_Map map = WarStageHelper.searchMap(property.Mapserialcode);
            response.map = StageFacade.toMap(map);
            response.stages = bodies;
            response.Succeed = true;
            response.hasbattle = RveStageController.Instance.HasJoinStage(userCode);
            return response;
        }

        public Entities.Usr_Stage trySearchStageByIndex(List<Entities.Usr_Stage> stages, long stageIndex)
        {
            foreach (Entities.Usr_Stage stage in stages)
            {
                if (stage.Stageindex == stageIndex)
                    return stage;
            }

            return null;
        }

        public Entities.Usr_Stage trySearchStageByCode(List<Entities.Usr_Stage> stages, long stageSerialcode)
        {
            foreach (Entities.Usr_Stage stage in stages)
            {
                if (stage.Stageserialcode == stageSerialcode)
                    return stage;
            }

            return null;
        }
    }


    public interface stage_list : ISangoResponse
    {
        List<stage_stage> stages { get;set;}
        stage_map map { get;set;}

        /// <summary>
        /// 是否有rve战斗
        /// </summary>
        bool hasbattle { get;set;}
        /// <summary>
        /// 是否有rve战斗结果
        /// </summary>
        bool hasbattleresult { get;set;}
    }
}
